import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
# from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

url = "127.0.0.1:4723/wd/hub"
param = {
  "deviceName": "127.0.0.1:62001",
  "platformName": "Android",
  "platformVersion": "7.1.2",
  "appPackage": "com.sina.weibo",
  "appActivity": "com.sina.weibo.SplashActivity"
}

driver = webdriver.Remote(url, param)
time.sleep(3)
# 点击同意并继续
el1 = WebDriverWait(driver, 25).until(lambda x: x.find_element(MobileBy.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.TextView[3]"))
# el1 = driver.find_element(MobileBy.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.TextView[3]")
el1.click()
time.sleep(3)
# 点击允许获取存储位置
TouchAction(driver).tap(x=646, y=914).perform()
time.sleep(2)
# 点击发现
el2 = WebDriverWait(driver, 25).until(lambda x: x.find_element(MobileBy.XPATH, "//android.widget.FrameLayout[@content-desc='发现']"))
# el2 = driver.find_element(MobileBy.XPATH, "//android.widget.FrameLayout[@content-desc='发现']")
el2.click()
time.sleep(1)
# 点击搜索框
el3 = WebDriverWait(driver, 25).until(lambda x: x.find_element(MobileBy.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.TabHost/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.EditText"))
# el3 = driver.find_element(MobileBy.XPATH, "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.TabHost/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.EditText")
el3.click()
time.sleep(1)
# 输入搜索黄渤
el4 = WebDriverWait(driver, 25).until(lambda x: x.find_element(MobileBy.ID, "com.sina.weibo:id/tv_search_keyword"))
# el4 = driver.find_element(MobileBy.ID, "com.sina.weibo:id/tv_search_keyword")
el4.send_keys("黄渤")
time.sleep(1)
# 点击黄渤
el5 = WebDriverWait(driver, 25).until(lambda x: x.find_element(MobileBy.ID, "com.sina.weibo:id/content_layout"))
# el5 = driver.find_element(MobileBy.ID, "com.sina.weibo:id/content_layout")
el5.click()
time.sleep(3)
# 滑动页面
for i in range(5):
    time.sleep(2)
    TouchAction(driver)   .press(x=797, y=1206)   .move_to(x=797, y=494)   .release()   .perform()


